//
//  ZZScrollView.m
//  ZZScrollerView
//
//  Created by 大展 on 16/6/22.
//  Copyright © 2016年 大展. All rights reserved.
//

#import "ZZScrollView.h"

#define kWidthFrame self.frame.size.width
#define kHeightFrame self.frame.size.height

@interface ZZScrollView ()<UIScrollViewDelegate>

// 滚动视图
@property (nonatomic, strong)UIScrollView *scrollView;
// 左图片
@property (nonatomic, strong)UIImageView *leftImageView;
// 右图片
@property (nonatomic, strong)UIImageView *rightImageView;
// 图片数组
@property (nonatomic, strong)NSArray *imageArray;
// 当前下标
@property (nonatomic, assign)NSInteger index;
// 小圆点
@property (nonatomic, strong)UIPageControl *pageControl;
// 定时器
@property (nonatomic, strong)NSTimer *timer;

@end

@implementation ZZScrollView

- (NSTimer *)timer {

    if (!_timer) {

        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (UIScrollView *)scrollView {

    if (!_scrollView) {

        // 创建滚动视图 及属性
       _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidthFrame, kHeightFrame)];
        _scrollView.contentSize = CGSizeMake(kWidthFrame*2, kHeightFrame);
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl {

    if (!_pageControl) {

        // 创建小圆点
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kHeightFrame-50, 100, 50)];
        _pageControl.numberOfPages = self.imageArray.count;
    }
    return _pageControl;
}

- (UIImageView *)leftImageView {

    if (!_leftImageView) {
         _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidthFrame, kHeightFrame)];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString: self.imageArray[0]]];
    }
    return _leftImageView;
}

- (UIImageView *)rightImageView {

    if (!_rightImageView) {

        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidthFrame, 0, kWidthFrame, kHeightFrame)];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString: self.imageArray[self.index]]];
    }
    return _rightImageView;
}

- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArrary
{
    self = [super initWithFrame:frame];
    if (self) {

        self.index = 1;
        // 给数组赋值
        self.imageArray = imageArrary;
        // 设置视图
        [self createUI];
    }
    return self;
}

- (void)createUI {

    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];

    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self.timer fire];
    });
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.x == kWidthFrame) {

        scrollView.contentOffset = CGPointZero;

        self.pageControl.currentPage = self.index;

        self.index++;
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString: self.imageArray[self.index]]];
    }
}

// 定时器事件
- (void)timerAction {

    [self.scrollView setContentOffset:CGPointMake(kWidthFrame, 0) animated:YES];
}



@end
